﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LybrixNursingCommon;
using LybrixNursingCRMDesktop.FrameWork;

namespace LybrixNursingCRMDesktop
{
    /// <summary>
    /// Interaction logic for ProspectStatusList.xaml
    /// </summary>



    public partial class ProspectStatusList : Page
    {
        private NursingDBContainer _entities;
        private customer currentCustomer = null;

        Frame MainFrame;

        public ProspectStatusList()
        {
            InitializeComponent();
            _entities = Repository.Entities;
            intializComboboxes();

            if ((bool)Login.loggedUser.isAgent && (!(bool)(Login.loggedUser.isSuperUser) || !(bool)(Login.loggedUser.isAdmin)))
            {
                agentName.SetItem(Login.loggedUser);
                agentName.IsEnabled = false;
            }
            PerformSearch(true);
        }




        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            PerformSearch(false);
        }
        private void intializComboboxes()
        {
            customerlocation.SetItemsSource = null;
            agentName.SetItemsSource = null;
            vendor.SetItemsSource = null;

            agentName.DisplayMemberPath("firstName");
            agentName.SelectedValuePath("Id");
            agentName.SetItemsSource = _entities.agents;

            vendor.DisplayMemberPath("sapakName");
            vendor.SelectedValuePath("Id");
            vendor.SetItemsSource = _entities.Sapaks;


            customerlocation.DisplayMemberPath("Descr");
            customerlocation.SelectedValuePath("Id");
            customerlocation.SetItemsSource = new List<ComboboxItem> { 
                new ComboboxItem( "-1",""), 
                new ComboboxItem("1","בית חולים"), 
                new ComboboxItem("2","שיקום"), 
                new ComboboxItem("3","בית")
            };


            customerstatus.DisplayMemberPath("Descr");
            customerstatus.SelectedValuePath("Id");
            customerstatus.SetItemsSource = new List<ComboboxItem> { 
                new ComboboxItem( "-1",""), 
                new ComboboxItem("1","רווק"), 
                new ComboboxItem("2","נשוי"), 
                new ComboboxItem("3","גרוש/ה"), 
                new ComboboxItem("4","אלמן/ה") 
            };


            teekstatus.DisplayMemberPath("Descr");
            teekstatus.SelectedValuePath("Id");
            teekstatus.SetItemsSource = new List<ComboboxItem> { 
                new ComboboxItem( "-1",""),  
                new ComboboxItem("3","נקלט"), 
                new ComboboxItem("4","סגור")
                };

            Healthstatus.DisplayMemberPath("Descr");
            Healthstatus.SelectedValuePath("Id");
            Healthstatus.SetItemsSource = new List<ComboboxItem> { 
                new ComboboxItem("-1", ""), 
                new ComboboxItem("1", "עצמאי/ת"), 
                new ComboboxItem("2", "תשוש גוף"), 
                new ComboboxItem("3", "סיעודי דימנטי"), 
                new ComboboxItem("4", "סיעודי כללי") ,
                new ComboboxItem("5", "סיעודי מורכב") ,
                new ComboboxItem("6", "נפטר/ה") ,
                new ComboboxItem("7", "תשוש נפש") ,
                new ComboboxItem("8", "לא ידוע")
            };
        }
        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            ProspectFName.ClearValue();
            ProspectLName.ClearValue();
            agentName.ClearValue();
            teekstatus.ClearValue();
            customerstatus.ClearValue();
            customerlocation.ClearValue();
            Healthstatus.ClearValue();
            vendor.ClearValue();

        }

        private void PerformSearch(bool isInitial)
        {
            IQueryable<Teek> teeks = null;

            teeks = (from x in _entities.Teeks 
                            where  (x.status == "3" || x.status == "4") select x);
            if (isInitial)
            {
                if ((bool)Login.loggedUser.isAgent && (!(bool)Login.loggedUser.isSuperUser || !(bool)Login.loggedUser.isAdmin))
                {
                    
                    if (agentName.GotValue)
                    {
                        var comprarer = agentName.Value as agent;
                        teeks = teeks.Where(x => x.agent.Id == comprarer.Id);
                    }
                }
            }
            else
            {

                if (ProspectFName.GotValue)
                {
                    teeks = (from t1 in teeks

                             where t1.customer.firstName.Contains(ProspectFName.Value)
                             select t1);
                }
                if (ProspectLName.GotValue)
                {
                    teeks = teeks.Where(x => x.customer.lastName.Contains(ProspectLName.Value));
                }

                if (agentName.GotValue)
                {
                    var comprarer = agentName.Value as agent;
                    teeks = teeks.Where(x => x.agent.Id == comprarer.Id);
                }
                if (teekstatus.GotValue)
                {
                    var comprarer = teekstatus.Value as ComboboxItem;
                    teeks = teeks.Where(x => x.status == comprarer.Id);
                }

                if (customerstatus.GotValue)
                {
                    var comprarer = customerstatus.Value as ComboboxItem;
                    teeks = teeks.Where(x => x.customer.familyStatus == comprarer.Id);
                }

                if (customerlocation.GotValue)
                {
                    var comprarer = customerlocation.Value as ComboboxItem;
                    teeks = teeks.Where(x => x.customer.location == comprarer.Id);
                }

                if (fromOpendt.GotValue)
                {
                    teeks = teeks.Where(x => x.customer.teekOpen_date >= fromOpendt.Value);
                }

                if (toOpendt.GotValue)
                {
                    teeks = teeks.Where(x => x.customer.teekOpen_date <= toOpendt.Value);
                }

                if (ContactId.GotValue)
                {
                    string comprarer = ContactId.Value;

                    teeks = from x in teeks
                            join tc in _entities.TeekContacts on x.Id equals tc.TeekId
                            where tc.contactName == comprarer
                            select x;

                }

                if (vendor.GotValue)
                {
                    var comprarer = vendor.Value as Sapak;
                    teeks = teeks.Where(x => x.Sapak.Id == comprarer.Id);
                }

            }
            OldProspectListView.ItemsSource = null;
            OldProspectListView.ItemsSource = teeks;
            if (teeks != null)
            {
                resultlbl.SetSearchLabel(teeks.Count());
            }
            else
                resultlbl.SetSearchLabel(0);
        }

        private void ProspectListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            NavigationCenter.ShowDetailPage(currentCustomer, NavigationTarget.TeekDetails, this);
            e.Handled = true;
        }

        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var dep = (DependencyObject)e.OriginalSource;
            while ((dep != null) && !(dep is ListViewItem))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }
            if (dep == null)
                return;
            currentCustomer = ((Teek)OldProspectListView.ItemContainerGenerator.ItemFromContainer(dep)).customer;

        }

        private void agentName_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }








}
